package cn.iocoder.yudao.module.system.controller.admin.cylinderinfo;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.system.controller.admin.cylinderinfo.vo.*;
import cn.iocoder.yudao.module.system.dal.dataobject.cylinderinfo.CylinderInfoDO;
import cn.iocoder.yudao.module.system.service.cylinderinfo.CylinderInfoService;

@Tag(name = "管理后台 - 钢瓶信息")
@RestController
@RequestMapping("/system/cylinder-info")
@Validated
public class CylinderInfoController {

    @Resource
    private CylinderInfoService cylinderInfoService;

    @PostMapping("/create")
    @Operation(summary = "创建钢瓶信息")
    @PreAuthorize("@ss.hasPermission('system:cylinder-info:create')")
    public CommonResult<Long> createCylinderInfo(@Valid @RequestBody CylinderInfoSaveReqVO createReqVO) {
        return success(cylinderInfoService.createCylinderInfo(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新钢瓶信息")
    @PreAuthorize("@ss.hasPermission('system:cylinder-info:update')")
    public CommonResult<Boolean> updateCylinderInfo(@Valid @RequestBody CylinderInfoSaveReqVO updateReqVO) {
        cylinderInfoService.updateCylinderInfo(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除钢瓶信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:cylinder-info:delete')")
    public CommonResult<Boolean> deleteCylinderInfo(@RequestParam("id") Long id) {
        cylinderInfoService.deleteCylinderInfo(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得钢瓶信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:cylinder-info:query')")
    public CommonResult<CylinderInfoRespVO> getCylinderInfo(@RequestParam("id") Long id) {
        CylinderInfoDO cylinderInfo = cylinderInfoService.getCylinderInfo(id);
        return success(BeanUtils.toBean(cylinderInfo, CylinderInfoRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得钢瓶信息分页")
    @PreAuthorize("@ss.hasPermission('system:cylinder-info:query')")
    public CommonResult<PageResult<CylinderInfoRespVO>> getCylinderInfoPage(@Valid CylinderInfoPageReqVO pageReqVO) {
        PageResult<CylinderInfoDO> pageResult = cylinderInfoService.getCylinderInfoPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CylinderInfoRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出钢瓶信息 Excel")
    @PreAuthorize("@ss.hasPermission('system:cylinder-info:export')")
    @OperateLog(type = EXPORT)
    public void exportCylinderInfoExcel(@Valid CylinderInfoPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CylinderInfoDO> list = cylinderInfoService.getCylinderInfoPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "钢瓶信息.xls", "数据", CylinderInfoRespVO.class,
                        BeanUtils.toBean(list, CylinderInfoRespVO.class));
    }

}